package xiangya.bysj.client.controller;

import java.util.HashMap;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import xiangya.bysj.entity.Cache;
import xiangya.bysj.service.impl.CacheServiceImpl;
import xiangya.bysj.service.impl.PatientServiceImpl;
import xiangya.bysj.util.StringUtil;

@Controller
@RequestMapping(value = "/client/cache", produces = "application/json;charset=UTF-8")
public class CacheClientController {

	@Resource
	private PatientServiceImpl patientService;

	@Resource
	private CacheServiceImpl cacheService;

	@RequestMapping("/update")
	@ResponseBody
	public String update(@RequestParam(value = "data", required = false) String data,
			@RequestParam(value = "doctorAccount", required = false) String doctorAccount,
			@RequestParam(value = "patientId", required = false) int patientId) {
		String result = StringUtil.RES_FAIL;
		try {

			Cache cache = new Cache();
			cache.setOperation(data);
			cache.setStatus(1);
			cache.setCtime(System.currentTimeMillis());

			cache.setUserAccount(patientService.getUserByPatientId(patientId).getUserAccount());
			cache.setDoctorAccount(doctorAccount);

			/**
			 * 构造阿里百川的消息
			 */
			cacheService.saveUpdateTakeinfos(cache, null);
			result = StringUtil.RES_SUCCESS;

		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;

	}

	@RequestMapping("/uncheckedCache")
	@ResponseBody
	public Object getUncheckedCache(@RequestParam(required = false, value = "userAccount") String userAccount) {

		HashMap<String, Object> object = new HashMap<String, Object>();
		try {
			object.put("caches", cacheService.getCacheByUserAccount(userAccount));

		} catch (Exception e) {
			e.printStackTrace();
		}

		return object;
	}

	@RequestMapping("/cancel")
	@ResponseBody
	public String cancel(@RequestParam(value = "fid", required = false) int fid) {
		String result = StringUtil.RES_FAIL;
		try {
			cacheService.cancel(fid);
			result = StringUtil.RES_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@RequestMapping("/confirm")
	@ResponseBody
	public String confirm(@RequestParam(value = "fid", required = false) int fid) {
		String result = StringUtil.RES_FAIL;
		try {
			cacheService.confirm(fid);
			result = StringUtil.RES_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

}
